package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;

import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.PageBean;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired(required = false)
    private OrderService orderService;


    /**
     * 作用:下单
     *
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session) {
        //1.得到登录者
        Long userId = (Long) session.getAttribute("user");
        //3.下单
        orderService.submit(orders, userId);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<PageBean<OrdersDto>> userPage(Integer page, Integer pageSize) {
        PageBean<OrdersDto> pageResult=orderService.userPage(page,pageSize);
        return R.success(pageResult);

    }

    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, String number ,String beginTime,String endTime) {
        Page<Orders> pageResult =orderService.findByNumberAndTime(page,pageSize,number,beginTime,endTime);
        return R.success(pageResult);
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders ,HttpSession session){
        //得到登录者
        //1.得到登录者
        Long userId = (Long) session.getAttribute("user");
        orderService.again(orders,userId);
        return R.success("成功再来一单");




    }

}
